package com.neusoft.service.impl;

import com.neusoft.mapper.MedicalRecordDiseaseMapper;
import com.neusoft.mapper.MedicalRecordMapper;
import com.neusoft.service.MedicalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MedicalRecordServiceImpl implements MedicalRecordService {
    @Autowired
    MedicalRecordMapper medicalRecordMapper;
    @Autowired
    MedicalRecordDiseaseMapper medicalRecordDiseaseMapper;
    @Override
    public String saveMedicalRecord(Map<String, Object> medicalRecord) {
        //保存病例到medical_record中
        medicalRecordMapper.insert(medicalRecord);
        //保存诊断疾病id号到中间表medical_record_disease中
        List<Map<String,Object>> diseaseList =(List<Map<String,Object>>) medicalRecord.get("diseaseList");
        if(diseaseList.size()>0){
            for(Map<String,Object> disease:diseaseList){
                HashMap<String, Object> medicalRecordDisease = new HashMap<>();
                medicalRecordDisease.put("medical_record_id",medicalRecord.get("id"));
                medicalRecordDisease.put("disease_id",disease.get("id"));
                medicalRecordDiseaseMapper.insert(medicalRecordDisease);
            }

        }
        return "保存病例成功";

    }
    @Override
    public Map<String, Object> selectMedicalRecord(String id) {
        //先查询病例
        Map<String, Object> medicalRecordMap = medicalRecordMapper.selectMedicalRecord(Integer.parseInt(id));
        //然后通过病例id查询对应的疾病信息
        List<Map<String, Object>> diseaseList = medicalRecordMapper.selectDiseaseList(Integer.parseInt(medicalRecordMap.get("id").toString()));
        medicalRecordMap.put("diseaseList",diseaseList);
        return medicalRecordMap;
    }
    @Override
    public String updateMedicalRecord(Map<String, Object> map) {
        map.remove("diseaseList");
        medicalRecordMapper.update(map);
        return "success";
    }

//y
    @Override
    public String saveDiagnose(Map<String, Object> medicalDiagnosis) {
        medicalRecordMapper.updateDiagnosis(medicalDiagnosis);
        return "保存病历成功";
    }
}
